package com.oepm.web.controller;

import com.oepm.biz.TimeBiz;
import com.oepm.biz.UserBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.TimePo;
import com.oepm.dao.entity.UserPo;
import com.oepm.common.utils.RoleUtil;
import com.oepm.web.vo.LoginInfoVo;
import com.oepm.web.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.*;

@CrossOrigin//解决跨域
@RestController
@Api(tags = "用户登录")
public class LoginController {

    @Resource
    private UserBiz userBiz;
    @Resource
    private TimeBiz timeBiz;

    /**
     * 登录--返回用户拥有角色
     * @param
     * @return
     */
    @PostMapping("/login/getRole")
    public Result loginRoles(@RequestBody LoginInfoVo loginInfoVo){
        String roleId = userBiz.queryRoleId(loginInfoVo.getUserId());
        if(roleId.contains(",")){
            String[] split = roleId.split("\\,");
            List<String> list = Arrays.asList(split);
            Collections.sort(list);
            roleId = String.join(",", list);
        }
        Map<String,String> map = new HashMap<>();
        map.put("roleId",roleId);
        Result result;
        if (roleId == null){
            result = new Result(Meta.FAIL);
        }else{
            result = new Result(Meta.SUCCESS);
            result.setData(map);
        }
        return result;
    }

    /**
     * 登录
     * @param loginInfo
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping("/login")
    @ApiOperation("用户登录模块")
    public Result login(@RequestBody LoginInfoVo loginInfo) throws UnsupportedEncodingException {
        UserPo user = userBiz.queryUserByUserId(loginInfo.getUserId());
        if(!user.getPassword().equals(loginInfo.getPassword())){
            return new Result(Meta.FAIL);
        }
        if(loginInfo.getRoleone()==null | loginInfo.getRoleone().equals("")){
            return new Result(Meta.FAIL);
        }
        //当前用户角色名
        String roleId =RoleUtil.roleChange(loginInfo.getRoleone());


        user.setRoleId(roleId);
        //多角色选择处理学院
        if(loginInfo.getRoleone().equals("校区管理员") | loginInfo.getRoleone().equals("1")){
            user.setAcademy(null);
        }
        String token = JWTUtil.createToken(user);
        LoginVo loginVo = new LoginVo(user,loginInfo.getRoleone(),token);
        List<TimePo> list = timeBiz.selectAll();
        if (list!=null){
            for (TimePo time : list){
                //立项时间
                loginVo.setStartTime(time.getStartTime());
                //普通项目截至时间
                loginVo.setEndTimeP(time.getEndTimeP());
                //重点项目截至时间
                loginVo.setEndTimeZ(time.getEndTimeZ());
            }
        }
        Result result = new Result(Meta.SUCCESS);
        result.setData(loginVo);
        System.out.println(loginVo);
        return result;
    }
}
